Rectangle with centered text in SVG

I would have expected that there is a standard primitive for including text in a rectangle in Scalable Vector Graphics (SVG). Sadly, there isn‘t. Here‘s how you can do it when generating SVG content in Python:

<g>
  <rect x="{x}" y="{y}" width="{w}" height="{h}"/>
  <text x="{x + w/2}" y="{y + h/2}" text-anchor="middle" dominant-baseline="middle"
</g>