It's not that straightforward, though. The purpose of the law is to protect the weakest in society. In this instance there's an argument - I'm not saying I agree with it btw - that the person who needs protection is the person with the illness. You're quite right, though. Not all mentally ill people say what he said. And, similar to the John Terry case, I'd argue that the language used doesn't just pop into your head from nowhere. It's usually part of who you are or has formed part of the currency of your home life or upbringing, whether or not you actually use the language openly.
However, to suggest that him making the comment had nothing to do with his illness(es) might not be true. He may well have said something in a momentary loss of self-control, the wording of which may have reflected his intent to hurt the other person rather than express his own views. None of which says he shouldn't have been prosecuted, tbf. But equally, Courts and we as society, are fully entitled to look at the entirety of the circumstances and decide that the act complained of doesn't necessarily reflect that person and that they should be given an opportunity to show that. It's a very complex question. You can end up with the victim feeling that the perpetrator hasn't been sufficiently punished. But the law has to take the larger view, rather than favouring punishments that are subjectively-pleasing to victims (though they'll often be one and the same).
I suppose, long story short, I think it's probably right that he was charged, but could also have seen some small amount of rationale behind not doing so, or at least affording him the opportunity of presenting the unique context that led to him doing what he did.