I too have attempted to rationalize this behavior that exists in nearly every human being (some more than others).

My best guess is it is some residual behavior that had benefits in evolution. There is power in numbers, and perhaps the creatures that find a bond based on a similarity (no matter how arbitrary) are more likely to survive the hardships of evolution.

I suppose if an early human lacked this similarity with the rest they could be placed into the "them" group and possibly killed eliminating that strain of DNA.

As bright as many of the people in the world are, it is shocking how few seem to be able to realize they do this. In some ways it handicaps their full potential.