title: How to set the coordinates of the output of xarray.assign?
body: <p>I've been trying to create two new variables based on the latitude coordinate of a data point in an xarray dataset. However, I can only seem to assign new coordinates. The data set looks like this:</p>
  <pre><code>&lt;xarray.Dataset&gt;
  Dimensions:  (lon: 360, lat: 180, time: 412)
  Coordinates:
    * lon      (lon) float64 0.5 1.5 2.5 3.5 4.5 ... 355.5 356.5 357.5 358.5 359.5
    * lat      (lat) float64 -89.5 -88.5 -87.5 -86.5 -85.5 ... 86.5 87.5 88.5 89.5
    * time     (time) datetime64[ns] 1981-09-01 1981-10-01 ... 2015-12-01
  Data variables:
      evapr    (time, lat, lon) float32 ...
      lhtfl    (time, lat, lon) float32 ...
  ...
  </code></pre>
  <p>What I've attempted so far is this:</p>
  <pre class="lang-py prettyprint-override"><code>def get_latitude_band(latitude):
      return np.select(
          condlist=
          [abs(latitude) &lt; 23.45,
           abs(latitude) &lt; 35,
           abs(latitude) &lt; 66.55],
          choicelist=
          [&quot;tropical&quot;,
           &quot;sub_tropical&quot;,
           &quot;temperate&quot;],
          
          default=&quot;frigid&quot;
      )
  
  def get_hemisphere(latitude):
      return np.select(
          [latitude &gt; 0, latitude &lt;=0],
          [&quot;north&quot;, &quot;south&quot;]
      )
  
      
  mhw_data = mhw_data \
      .assign(climate_zone=get_latitude_band(mhw_data.lat)) \
      .assign(hemisphere=get_hemisphere(mhw_data.lat)) \
      .reset_index([&quot;hemisphere&quot;, &quot;climate_zone&quot;]) \
      .reset_coords()
              
  print(mhw_data)
  </code></pre>
  <p>Which is getting me close:</p>
  <pre><code>&lt;xarray.Dataset&gt;
  Dimensions:        (lon: 360, lat: 180, time: 412, hemisphere: 180, climate_zone: 180)
  Coordinates:
    * lon            (lon) float64 0.5 1.5 2.5 3.5 4.5 ... 356.5 357.5 358.5 359.5
    * lat            (lat) float64 -89.5 -88.5 -87.5 -86.5 ... 86.5 87.5 88.5 89.5
    * time           (time) datetime64[ns] 1981-09-01 1981-10-01 ... 2015-12-01
  Dimensions without coordinates: hemisphere, climate_zone
  Data variables:
      evapr          (time, lat, lon) float32 ...
      lhtfl          (time, lat, lon) float32 ...
      ...
      hemisphere_    (hemisphere) object 'south' 'south' ... 'north' 'north'
      climate_zone_  (climate_zone) object 'frigid' 'frigid' ... 'frigid' 'frigid'
  ...
  </code></pre>
  <p>However, I want to then stack the DataSet and convert it to a DataFrame. I am unable to do so, and I think it is because the new variables <code>hemisphere_</code> and <code>climate_zone_</code> do not have <code>time</code>, <code>lat</code>, <code>lon</code> coordinates:</p>
  <pre class="lang-py prettyprint-override"><code>stacked = mhw_data[var].stack(dim=[&quot;lon&quot;, &quot;lat&quot;, &quot;time&quot;]).to_pandas().T
  </code></pre>
  <p>results in a <code>KeyError</code> on &quot;lon&quot;.</p>
  <p>So my question is: <strong>How do I assign new variables to the xarray dataset that maintain the original coordinates of time, lat and lon?</strong></p>