How is it not allowed? Employers can’t choose who they want to hire? You’d obviously word it differently: unfortunately you’re not as good as another candidate. Better luck next time!
Employers can’t choose who they want to hire when that decision is related to union involvement, yes. You’re not the first person to think that hiding the real reason for employment discrimination somehow makes it legal. It is not.