Well, I mean, if you'd rather a political than a mechanics-of-law explanation as to why the law treats two situations differently mo, it's legal because heterosexual couples plus women outside of such couples combine to form a powerful voting bloc, and gender rating, compared to it's absence with no other change in practice, is a benefit to that group.
Specifically, compared to perfect allocation of costs, it's a small subsidy from people outside of opposite-sex couples to opposite-sex couples, whereas not having gender rating with otherwise status quo policies is a large subsidy from women not in opposite-sex couples to men not in opposite-sex couples.
Under status quo policies, opposite sex couples are in practice fairly free to gender-optimize insurance pricing by putting the less expensive to insure driver as primary on the more expensive to insure vehicle, regardless of actual primary driver.
Because car insurance isn't employment, is separately regulated, and does not exclude gender from permissible inputs into actuarial calculations.