If thy Lord had so willed, He could have made mankind one people: but they will not cease to dispute.
This is the answer to the objection raised in the name of taqdir (destiny) against the principle that has been laid down in the preceding verse for the destruction of the wicked communities. It is this:Why should these communities be punished for lack of a sufficient number of righteous persons in them? Why didn’t Allah Himself will to produce that sufficient number of righteous persons in it? Allah has answered this objection, so as to say:It is not the will of Allah to bind mankind inherently to a fixed course of life as He has bound the vegetable and animal life. Had it been so, there would have been no need of sending the Messengers and the Books for inviting mankind to the faith. For, in that case all human beings would have been born Muslims and born believers and there would have been no unbelief and no disobedience. But it is the will of Allah to grant freedom of choice and action to man so that he may be free to choose and follow any way of life. That is why He has left open both the way to Paradise and the way to Hell for man and given every individual and every community the opportunity to choose and follow either of the two ways with full freedom and win any of the two abodes as a result of his own efforts and exertions. It is obvious that this scheme of Allah demands that it should be based on the freedom of choice and the option to adopt faith or unbelief. This is why Allah does not compel a community to the right way if it itself intends to follow the wrong way, and plans and works for it. Allah’s scheme does not interfere with the plans and works of such a community. When it makes deliberate arrangements to mold the wicked, unjust, and sinful people, He does not supply it with righteous persons by birth to set it right. Every community is free to produce good or bad people as it wills. And if a community wills to follow a wrong way as a community, leaving little room for the production and the development of the righteous people in order to raise up the standard of virtue, God does not will to coerce it to follow the righteous way. He will let it follow the way it chooses for itself with its inevitable consequences. On the contrary, Allah shows His mercy to that community which produces a sufficiently large number of such people as accept the invitation to virtue, and leaves room in its collective system for such people to work for its reform and purification.
Maududi, Abul Ala (2010). Tafhim ul Quran. https://www.englishtafsir.com/Quran/27/index.html