In some philosophical accounts, “necessary” means that an effect must issue from a nature such that nonoccurrence is impossible. This is not the same as a moral or legal obligation.
If every supposedly best act issued from Allah through this sort of necessary emanation, choice and will would no longer function as genuinely distinct notions; the act would follow by necessity alone. The Sunni account instead affirms Allah as a voluntary agent.
Thus “Allah acts wisely” does not entail “every conceivable optimally beneficial act must emanate from Him without the possibility of its omission.”