Although I don't agree with the decision, and don't really rate Ansari as being een close to test class, I do sort of understand it.
If we agree that Ali is our notional number one spinner but will in fact take the fifth batting slot and come in at seven behind Bairstow (keeper) and Stokes (third/fourth seamer) with Woakes, Rashid, Broad and Anderson rounding out our "first" XI then you already hae a bit of a disconnect - Rashid is a great second innings bowler but will not contain in the first dig. Likewise, Ali can leak runs. If a bowler is left out because the pitch demands three spinners, you would assume that Woakes would make way. In that case, you want a) a bowler different to the other two spinners (which ruled out Rayner) and b) one who fills the gap in styles - so a containing bowler. Leach looks to flight the ball a lot, whereas Ansari has, when I've seen him, been ruthlessly economical - and he has the advantage of being a decent bat to boot.