This rule’s common in many traditionalist religious institutions/households and in Anglo-Saxon/European cultures going back to the Medieval era.
One supposed origin for this tradition was that it originated from a time hats were worn partially to shield the owner from incoming garbage/sewage being tossed out from the upper stories of castles/buildings onto the streets below. A common and yes, unsanitary practice back in that time.
Since the hats were likely covered in muck from the tossed garbage/sewage…especially if one was in a more densely populated area…continuing to wear one’s hat indoors would be like wearing muck ridden footwear indoors.*
- Likely an origin as to why some cultures require guests/residents to leave footwear worn outdoors in an area intended for such footwear near the door rather than wearing them indoors.