For an approach with transform, see this answer of mine to a similar question: https://stackoverflow.com/a/47860039/1709587
Alternatively, just using writing-mode: vertical-lr (or the deprecated writing-mode: td-lr) seems to work fine in modern versions of Chrome; the containing div will correctly accomodate the h2 within it, and no transforms are needed. See the example below: 
#container {
  /* Pink background for easier visualisation of the end result */
  background: pink;
  
  /* Optional; shrinks width of div to minimum required to contain h2 */
  display: inline-block;
}
#container h2 {
  /* Note that the td-lr value used in the question is deprecated;
     vertical-lr is the modern equivalent. See:
     https://developer.mozilla.org/en-US/docs/Web/CSS/writing-mode
     Consider also using
       -webkit-writing-mode: vertical-lr
     and
       -ms-writing-mode: td-lr
     for compatibility with older browsers. */
  writing-mode: vertical-lr;
  /* Optional; flips the heading, so that it is written bottom-to-top
     instead of top-to-bottom.
     Consider also using -webkit-transform and -moz-transform and
     -o-transform and an equivalent transform with filter for compatibility
     with old browsers.*/
  transform: scale(-1, -1);
  
  /* Optional: disable breaks in the title */
  white-space: nowrap;
}
<div id="container">
  <h2>Some text</h2>
</div>
<div>
  More stuff
</div>
 
 
However, you'll find a slew of layout bugs if you try to get this working on current (December 2017) versions of Firefox, Edge, or Safari; the four major browsers have fairly broken handling of vertical writing-modes at the moment. They're not consistent with each other, and they're frequently not even deterministic - you can toggle a style rule off and on again at the dev tools and end up with a differently laid-out page. As such, while this approach is an option, beware using it and test extensively if you do.