A union absolutely can and should protect workers from frivolous layoffs.
If the company tries to layoff 10% "due to AI" the remaining 90% can strike.
History is full of union solidarity vs idiotic management.