title: How can I replace values in an xarray variable?
body: <p>I have an xarray dataset <code>ds</code></p>
  
  <pre><code>&lt;xarray.Dataset&gt;
  Dimensions:         (elevation_band: 4, latitude: 1, longitude: 1)
  Coordinates:
    * longitude       (longitude) float64 -111.4
    * latitude        (latitude) float64 44.51
    * elevation_band  (elevation_band) int32 1 2 3 4
  Data variables:
      area_frac       (elevation_band, latitude, longitude) float64 0.005109 ...
      mean_elev       (elevation_band, latitude, longitude) float64 2.45e+03 ...
      prec_frac       (elevation_band, latitude, longitude) float64 0.005109 ...
  </code></pre>
  
  <p>And I want to replace the values of <code>mean_elev</code> with these values <code>[0.1, 0.2, 0.3, 0.4]</code> leading to this error:</p>
  
  <h1>Trial 1</h1>
  
  <pre><code>ds['mean_elev'].values = np.atleast_3d([0.1, 0.5, 0.3, 0.6]).reshape((4, 1, 1))
  </code></pre>
  
  <h3>Error 2</h3>
  
  <p><code>MissingDimensionsError: cannot set variable 'mean_elev' with 3-dimensional data without explicit dimension names. Pass a tuple of (dims, data) instead.</code></p>
  
  <h1>Trial 2</h1>
  
  <p>So, far I have tried to create a separate dataArray for merging:</p>
  
  <pre><code>lat = ds['latitude'].values
  long = ds['longitude'].values
  elevation_band = ds['elevation_band'].values
  
  mean_elev = np.array([0.1, 0.5, 0.3, 0.6]).reshape((4, 1, 1))
  
  xr.DataArray(mean_elev, coords={'latitude': lat, 'longitude': long, 
                                  'elevation_band': elevation_band},
               dims=['latitude', 'longitude', 'elevation_band'])
  </code></pre>
  
  <h3>Error 2</h3>
  
  <p><code>ValueError: conflicting sizes for dimension 'latitude': length 4 on the data but length 1 on coordinate 'latitude'</code></p>
  
  <p>Open to alternative solutions or making one of these work.</p>