While the human immune system is remarkable in its ability to respond to a myriad of foreign materials it encounters in the environment, unfortunately, it’s not perfect.
Sometimes it mounts a response to a harmless material, like animal dander, dust particles, pollens or foods; and the results are symptoms characteristic of hay fever, asthma, excema, or a food allergy, which can be pretty miserable for those who suffer from it.
Why does the body do this? If I had the answer to that, I’d be on my Nobel Prize lecture tour right now.
Certainly, genetics plays a big role. With the childhood risk of allergies associated with parental tendencies toward allergic diseases. But environmental influences and certain infections may also predispose some people toward allergies.
What’s the evolutionary purpose of the allergic response? Well it may have helped protect against parasitic disease when these were more common. But today, it’s mostly a leftover of unhelpful immune reaction making life more difficult for patients, but keeping us allergists pretty busy.