So, I received my totem Gumshoos and it had its hidden ability, I got totem Marowak and it had its hidden ability. My friend the other day received a totem Kommo-o and it had its hidden ability. Is this just luck or is it coded into the game?
Not all, but most of them. As for the exceptions, the Vikavolt, Araquanid, Lurantis, Salazzle, and Mimikyu that you receive all have their normal abilities.
Source
The foolproof rule is "Totems come with hidden abilities if, and only if, their species has two distinct regular abilities in slots 1 and 2." The point is to ensure that you can never legally use an Ability Capsule to change the ability of a totem.