Very few people are excommunicated these days. As I said before, if you try to provide a well-known example of this, I’m not sure you’d come up with much. In fact, folks wish the Church would excommunicate certain individuals, such as pro-abort, “Catholic” politicians, with much more regularity.
Now, a heretic is not someone who “speaks out,” but one who believes something that is contrary to revealed dogmas. The reason they can be excommunicated is that excommunication is medicinal, and is meant to wake a person up out of their stupor. Consciously, willingly, repeatedly, adamantly rejecting revealed truth means that you lack supernatural faith, which in turn implies you are not in a state of grace. This is dangerous, since such a soul will go to hell. Frankly, it’s evil to allow such a person to carry on without any attempt to help them.
Moreover, excommunication is meant to protect others against the errors of an individual, so that they aren’t lead away from the false teachings of someone who is manifestly a wolf amongst sheep.
In terms of not speaking out publicly, the problem is that a Catholic should be respectful of two things:
(1) Their own limitations. Someone who isn’t even educated in theology has no business making pronouncements. Each of us has a state in life, and not everyone is called to teach others. In our day and age, we all of us think we have a right to pontificate on any subject we wish, but this is utter applesauce. We have to have humility, and understand that knowing a lot about something is a complicated piece of work that takes years and years of study.
(2) The authority of the Church, which was established by Christ. No matter the individual intellect of a certain person, they are not able to out-think the collective mind of centuries of Church teaching, even on a natural level. But this is to say nothing of the supernatural level, wherein the Church is preserved from grave error when it makes known what is or is not revealed truth. Frankly, any Catholic who speaks out against the Church’s dogma, knowing full well what they’re doing, is someone who probably lacks supernatural faith, and is very likely, therefore, to be in a state of mortal sin. By their mortal sin, which they are apparently unrepentant of, they have already excommunicated themselves from Christ’s Body, since they reject the friendship of God.