Of course it can be.
Take a hypothetical variety of racism for instance: if you agree to paint yourself blue and preferentially aid others who are painted blue, people who painted themselves red and preferentially aided red-painted people would naturally prefer to not see you in an position of power or influence, as you would use that position to aid blues (and thus hinder reds who might otherwise have gotten the benefits you preferentially give to blues).
It would also, in this case, behoove you to not only discriminate in favor of blues, but against reds, as reds in power would, perforce, discriminate against you.
At some point, tension might increase until you were legitimately physically afraid of reds, because reds would see an advantage in harming you if they could get away with doing so.
While you'd know that some reds were actually peaceful, good people, you'd have no easy way to determine which reds were good, as it would be risky to associate with any reds, because the bad ones would take advantage of you.
As you became less and less willing to take the risk of trusting a red, so reds would observe your unwillingness to "reach out", and they would be logically inclined to not risk reaching out to you.
At some point, atrocities would be committed by either side: muggings, lynchings, rapes, and eventually a state of war might exist between red and blue.
With war, with the entire survival of one group or the other as a free people undominated by their enemies at stake, even "good" reds would be honor bound, for the survival of their race/people/nation to attempt to kill and harm even blues they knew to be "good", and vice versa: surely we have seen good and honorable men fight fiercely and honorably to kill other good and honorable men, when each side feels its children and way of life at stake.
So, yes, racism can be logical, because once racism starts, the short-term benefit is to rely on a stereotype rather than pay the cost and take the risk of examining people as people and not as members of groups. In the long term, this leads to positive reinforcement of both stereotypes and inter-group enmity -- but it can be awfully hard to endure high short-term costs for even high long-term rewards.
(And honestly, if you can commit genocide or enslave the opposition, your group will, by and large and up to the last century or two, also get a high reward: witness the genocide of Khoisan and to a lesser extent pygmy peoples in Africa by Bantu peoples, or of aboriginal Americans ("Indians") in both North and South America by Europeans, or the near-genocide of Basque and Celtic peoples by Indo-Europeans and Germanic peoples in mainland Europe. Or Armenians in Turkey in 1917. Genocide and enslavement are morally wrong, but they often greatly benefit the perpetrators: ask any American farmer where he got his land.)
If I know that 9 of 10 Maori will kill me because I am Mori (and indeed the peacerful Mori were wiped out by quite intentional Maori genocide and enslavement), I'd be a fool not kill every defenseless Maori I come across -- and a Maori, reasoning that I would so reason, would be a fool not to kill me.
(I will leave the logic of superstition to the reader, but I will hint that we all desire explanatory stories, and even Newtons' physics is subtly wrong, but good enough for most purposes involving human-perceptible masses and speeds. And that the religious, according to several studies, enjoy better health than atheists.)
Given that humans form kinship and pseudo-kinship ("clan", "tribe", "people", "nation") groups, and given that humans can model and predict the actions of other humans, xenophobia and racism are, regrettably, perhaps inevitable.
And recall that all of history's Cains had children, and the Abels did not; it is no surprise to this humanist and atheist that the mark of Cain is a standard and fundamental part of the heritage passed down to every human.