It’s generally best to avoid commenting on people’s hair for the following reasons:
If it’s a compliment, it can be taken as sexually invasive. In most settings, comments that indicate that you find someone sexually attractive are considered inappropriate, and violating rules about sexual decorum is likely to be seen as a threat. (Because breaking small rules about sexual boundaries is very often a prelude to breaking big ones). Compliments about someone’s body are risky at best, especially if you are male and the person you’re complimenting is female. Don’t do it without a really good reason.
Talking about hair can be racially loaded. In particular, white people are sometimes creepily fascinated by black people’s hair, and sometimes feel like they’re entitled to information about it. Sometimes they even feel like they’re entitled to touch it. People also sometimes are creepy about hair, and do invasive things like try to prove that they’re Totally Not Racist by complimenting a black person’s hair (or their decisions about how to wear it).
It’s really, really not pleasant to be on the receiving end of someone else’s attempt to prove they’re not a bigot. (Because this amounts to people trying to prove what good people they are *by violating other peoples’ boundaries*). Even if you have no such intention, talking about someone’s hair is likely to be very unwelcome because of this context.
Commenting on someone’s hair can also imply that you think the hair is there for you in some way. For instance, it’s really not ok to tell a woman that her long hair looks really good and you don’t understand why so many women cut off their beautiful hair. And a woman who is frequently told this is likely to hear the second part even if you only say the first part. It’s really, really not your business, and it’s worth avoiding the impression that you think it is.
It’s also not ok to tell someone with long hair that they should really donate it to Locks of Love. People have the right to keep parts of their body that they want to keep.
It can also be taken as a comment on someone’s gender identity. In particular, women with short hair often get harassed by people who think that they’re not proper women if their hair is that length. Men with long hair often get harassed by people who think they’re not being properly manly if they don’t cut it. Even if what you say is positive, there’s a good chance that they’re beyond sick of talking about it.
Commenting on children’s hair, or on hair belonging to people who aren’t treated as adults, is usually a bad idea because there’s a good chance that their hair reflects the unwelcome agenda imposed by another person. For example, little girls are often forced to have long hair that they hate because other people want them to look adorable. Boys are often made to have short hair because other people want them to look masculine, even if that’s not what they themselves want. It’s best to avoid assuming that their agenda and their parents’ agenda are the same, and best not to emote about how wonderfully masculine/feminine they look.