Plants usually don't like their seeds eaten (unless of course they're sure that you'll not digest it or somehow still help them reproduce). And we didn't co-evolve with grains much. We domesticated them fairly recently.
Unlike many other toxins plants put in their seeds to protect them, gluten doesn't seem to have been made just for defense. They just came up with a protein to feed their embryos... and turns out it was also toxic to herbivores. Of course, herbivores which co-evolved with these plants evolved ways to digest gluten... but when humans adopted it, we ran into problems.
The good thing is most of these problems can be avoided by not eating what plants don't want us to eat.