With series operations, things are dropped from the left side if there is no corresponding timestamp/datapoint in the right side. You can mix this with the dropbool function to avoid divide by zero:
$five = q("sum:rate{counter,,1}:haproxy.frontend.hrsp{}{status_code=5xx}", "1h", "")
$two = q("sum:rate{counter,,1}:haproxy.frontend.hrsp{}{status_code=2xx}", "1h", "")
$two / dropbool($five, ($five > 0))
It is possible after dropbool there will be an empty set which would also error. So series operations are recommended for visualization and for alerting it is recommended to use reduction functions earlier in the expression. Alternatively you could wrap the operation in the nv func after reduction: nv(avg($two / dropbool($five, ($five > 0))), 0)