diff --git a/docs/history.rst b/docs/history.rst
index 2732124..5448ef8 100644
--- a/docs/history.rst
+++ b/docs/history.rst
@@ -12,6 +12,7 @@ Latest
 - BUG: Return correct transform in `rio.transform` with non-rectilinear transform (discussions #280)
 - BUG: Update to handle WindowError in rasterio 1.2.2 (issue #286)
 - BUG: Don't generate x,y coords in `rio` methods if not previously there (pull #294)
+- BUG: Preserve original data type for writing to disk (issue #305)
 
 0.3.2
 -----
diff --git a/rioxarray/_io.py b/rioxarray/_io.py
index d4e664a..0f72ffb 100644
--- a/rioxarray/_io.py
+++ b/rioxarray/_io.py
@@ -812,6 +812,8 @@ def open_rasterio(
     encoding = {}
     if mask_and_scale and "_Unsigned" in attrs:
         unsigned = variables.pop_to(attrs, encoding, "_Unsigned") == "true"
+    if masked:
+        encoding["dtype"] = str(riods.dtypes[0])
 
     da_name = attrs.pop("NETCDF_VARNAME", default_name)
     data = indexing.LazilyOuterIndexedArray(
diff --git a/rioxarray/raster_array.py b/rioxarray/raster_array.py
index ce9a0cd..eeda38f 100644
--- a/rioxarray/raster_array.py
+++ b/rioxarray/raster_array.py
@@ -882,7 +882,11 @@ class RasterArray(XRasterBase):
         if driver is None and LooseVersion(rasterio.__version__) < LooseVersion("1.2"):
             driver = "GTiff"
 
-        dtype = str(self._obj.dtype) if dtype is None else dtype
+        dtype = (
+            self._obj.encoding.get("dtype", str(self._obj.dtype))
+            if dtype is None
+            else dtype
+        )
         # get the output profile from the rasterio object
         # if opened with xarray.open_rasterio()
         try:

diff --git a/test/integration/test_integration__io.py b/test/integration/test_integration__io.py
index 5444785..5bb6815 100644
--- a/test/integration/test_integration__io.py
+++ b/test/integration/test_integration__io.py
@@ -270,7 +270,11 @@ def test_open_rasterio_mask_chunk_clip():
         assert np.isnan(xdi.values).sum() == 52119
         test_encoding = dict(xdi.encoding)
         assert test_encoding.pop("source").endswith("small_dem_3m_merged.tif")
-        assert test_encoding == {"_FillValue": 0.0, "grid_mapping": "spatial_ref"}
+        assert test_encoding == {
+            "_FillValue": 0.0,
+            "grid_mapping": "spatial_ref",
+            "dtype": "uint16",
+        }
         attrs = dict(xdi.attrs)
         assert_almost_equal(
             tuple(xdi.rio._cached_transform())[:6],
@@ -307,7 +311,11 @@ def test_open_rasterio_mask_chunk_clip():
         _assert_xarrays_equal(clipped, comp_subset)
         test_encoding = dict(clipped.encoding)
         assert test_encoding.pop("source").endswith("small_dem_3m_merged.tif")
-        assert test_encoding == {"_FillValue": 0.0, "grid_mapping": "spatial_ref"}
+        assert test_encoding == {
+            "_FillValue": 0.0,
+            "grid_mapping": "spatial_ref",
+            "dtype": "uint16",
+        }
 
         # test dataset
         clipped_ds = xdi.to_dataset(name="test_data").rio.clip(
@@ -317,7 +325,11 @@ def test_open_rasterio_mask_chunk_clip():
         _assert_xarrays_equal(clipped_ds, comp_subset_ds)
         test_encoding = dict(clipped.encoding)
         assert test_encoding.pop("source").endswith("small_dem_3m_merged.tif")
-        assert test_encoding == {"_FillValue": 0.0, "grid_mapping": "spatial_ref"}
+        assert test_encoding == {
+            "_FillValue": 0.0,
+            "grid_mapping": "spatial_ref",
+            "dtype": "uint16",
+        }
 
 
 ##############################################################################
@@ -882,6 +894,7 @@ def test_mask_and_scale(open_rasterio):
                 "_FillValue": 32767.0,
                 "missing_value": 32767,
                 "grid_mapping": "crs",
+                "dtype": "uint16",
             }
             attrs = rds.air_temperature.attrs
             assert attrs == {
@@ -912,6 +925,7 @@ def test_no_mask_and_scale(open_rasterio):
             "_FillValue": 32767.0,
             "missing_value": 32767,
             "grid_mapping": "crs",
+            "dtype": "uint16",
         }
         attrs = rds.air_temperature.attrs
         assert attrs == {
diff --git a/test/integration/test_integration_rioxarray.py b/test/integration/test_integration_rioxarray.py
index ed4ae54..ebe6a73 100644
--- a/test/integration/test_integration_rioxarray.py
+++ b/test/integration/test_integration_rioxarray.py
@@ -1160,6 +1160,7 @@ def test_to_raster(
         assert_array_equal(rds.read(1), xds.fillna(xds.rio.encoded_nodata).values)
         assert rds.count == 1
         assert rds.tags() == {"AREA_OR_POINT": "Area", **test_tags, **xds_attrs}
+        assert rds.dtypes == ("int16",)
 
 
 @pytest.mark.parametrize(

Keep in memory original data type for writing
It could be very nice to keep in memory the original data type (ie. `uint16` for landsat data or `uint8` for classified data).

Indeed, for now (unless I missed something), if we open a dataset with `masked=True` and the data is casted to float, we lose the information of the original datatype.
So we always write the dataset on disk in float, even when it is not justified (when the nodata is replaced by its true value)
test/integration/test_integration__io.py::test_open_rasterio_mask_chunk_clip test/integration/test_integration_rioxarray.py::test_to_raster[None-False-True-True-open_method1] test/integration/test_integration_rioxarray.py::test_to_raster[None-False-True-True-open_method2] test/integration/test_integration_rioxarray.py::test_to_raster[None-False-True-True-open_method3] test/integration/test_integration_rioxarray.py::test_to_raster[None-False-True-True-open_rasterio_engine] test/integration/test_integration_rioxarray.py::test_to_raster[None-False-False-False-open_method1] test/integration/test_integration_rioxarray.py::test_to_raster[None-False-False-False-open_method2] test/integration/test_integration_rioxarray.py::test_to_raster[None-False-False-False-open_method3] test/integration/test_integration_rioxarray.py::test_to_raster[None-False-False-False-open_rasterio_engine] test/integration/test_integration_rioxarray.py::test_to_raster[write_lock1-False-True-True-open_method1] test/integration/test_integration_rioxarray.py::test_to_raster[write_lock1-False-True-True-open_method2] test/integration/test_integration_rioxarray.py::test_to_raster[write_lock1-False-True-True-open_method3] test/integration/test_integration_rioxarray.py::test_to_raster[write_lock1-False-True-True-open_rasterio_engine] test/integration/test_integration_rioxarray.py::test_to_raster[write_lock1-False-False-False-open_method1] test/integration/test_integration_rioxarray.py::test_to_raster[write_lock1-False-False-False-open_method2] test/integration/test_integration_rioxarray.py::test_to_raster[write_lock1-False-False-False-open_method3] test/integration/test_integration_rioxarray.py::test_to_raster[write_lock1-False-False-False-open_rasterio_engine] test/integration/test_integration_rioxarray.py::test_to_raster[write_lock2-True-True-True-open_method1] test/integration/test_integration_rioxarray.py::test_to_raster[write_lock2-True-True-True-open_method2] test/integration/test_integration_rioxarray.py::test_to_raster[write_lock2-True-True-True-open_method3] test/integration/test_integration_rioxarray.py::test_to_raster[write_lock2-True-True-True-open_rasterio_engine] test/integration/test_integration_rioxarray.py::test_to_raster[write_lock2-True-False-False-open_method1] test/integration/test_integration_rioxarray.py::test_to_raster[write_lock2-True-False-False-open_method2] test/integration/test_integration_rioxarray.py::test_to_raster[write_lock2-True-False-False-open_method3] test/integration/test_integration_rioxarray.py::test_to_raster[write_lock2-True-False-False-open_rasterio_engine]
test/integration/test_integration__io.py::test_build_subdataset_filter[netcdf:../../test/test_data/input/PLANET_SCOPE_3D.nc:blue-green-None-False] test/integration/test_integration__io.py::test_build_subdataset_filter[netcdf:../../test/test_data/input/PLANET_SCOPE_3D.nc:blue-blue-None-True] test/integration/test_integration__io.py::test_build_subdataset_filter[netcdf:../../test/test_data/input/PLANET_SCOPE_3D.nc:blue1-blue-None-False] test/integration/test_integration__io.py::test_build_subdataset_filter[netcdf:../../test/test_data/input/PLANET_SCOPE_3D.nc:1blue-blue-None-False] test/integration/test_integration__io.py::test_build_subdataset_filter[netcdf:../../test/test_data/input/PLANET_SCOPE_3D.nc:blue-blue-gr-False] test/integration/test_integration__io.py::test_build_subdataset_filter[HDF4_EOS:EOS_GRID:"./modis/MOD09GQ.A2017290.h11v04.006.NRT.hdf":MODIS_Grid_2D:sur_refl_b01_1-variable5-None-True] test/integration/test_integration__io.py::test_build_subdataset_filter[HDF4_EOS:EOS_GRID:"./modis/MOD09GQ.A2017290.h11v04.006.NRT.hdf":MODIS_Grid_2D:sur_refl_b01_1-None-group6-True] test/integration/test_integration__io.py::test_build_subdataset_filter[HDF4_EOS:EOS_GRID:"./modis/MOD09GQ.A2017290.h11v04.006.NRT.hdf":MODIS_Grid_2D:sur_refl_b01_1-variable7-group7-True] test/integration/test_integration__io.py::test_build_subdataset_filter[HDF4_EOS:EOS_GRID:"./modis/MOD09GQ.A2017290.h11v04.006.NRT.hdf":MODIS_Grid_2D:sur_refl_b01_1-blue-gr-False] test/integration/test_integration__io.py::test_build_subdataset_filter[HDF4_EOS:EOS_GRID:"./modis/MOD09GQ.A2017290.h11v04.006.NRT.hdf":MODIS_Grid_2D:sur_refl_b01_1-sur_refl_b01_1-gr-False] test/integration/test_integration__io.py::test_build_subdataset_filter[HDF4_EOS:EOS_GRID:"./modis/MOD09GQ.A2017290.h11v04.006.NRT.hdf":MODIS_Grid_2D:sur_refl_b01_1-None-gr-False] test/integration/test_integration__io.py::test_build_subdataset_filter[HDF4_EOS:EOS_GRID:"./modis/MOD09GQ.A2017290.h11v04.006.NRT.hdf"://MODIS_Grid_2D://sur_refl_b01_1-sur_refl_b01_1-None-True] test/integration/test_integration__io.py::test_build_subdataset_filter[HDF4_EOS:EOS_GRID:"./modis/MOD09GQ.A2017290.h11v04.006.NRT.hdf"://MODIS_Grid_2D://sur_refl_b01_1-None-MODIS_Grid_2D-True] test/integration/test_integration__io.py::test_build_subdataset_filter[HDF4_EOS:EOS_GRID:"./modis/MOD09GQ.A2017290.h11v04.006.NRT.hdf"://MODIS_Grid_2D://sur_refl_b01_1-sur_refl_b01_1-MODIS_Grid_2D-True] test/integration/test_integration__io.py::test_build_subdataset_filter[HDF4_EOS:EOS_GRID:"./modis/MOD09GQ.A2017290.h11v04.006.NRT.hdf"://MODIS_Grid_2D://sur_refl_b01_1-blue-gr-False] test/integration/test_integration__io.py::test_build_subdataset_filter[HDF4_EOS:EOS_GRID:"./modis/MOD09GQ.A2017290.h11v04.006.NRT.hdf"://MODIS_Grid_2D://sur_refl_b01_1-sur_refl_b01_1-gr-False] test/integration/test_integration__io.py::test_build_subdataset_filter[HDF4_EOS:EOS_GRID:"./modis/MOD09GQ.A2017290.h11v04.006.NRT.hdf"://MODIS_Grid_2D://sur_refl_b01_1-None-gr-False] test/integration/test_integration__io.py::test_build_subdataset_filter[netcdf:S5P_NRTI_L2__NO2____20190513T181819_20190513T182319_08191_01_010301_20190513T185033.nc:/PRODUCT/tm5_constant_a-None-PRODUCT-True] test/integration/test_integration__io.py::test_build_subdataset_filter[netcdf:S5P_NRTI_L2__NO2____20190513T181819_20190513T182319_08191_01_010301_20190513T185033.nc:/PRODUCT/tm5_constant_a-tm5_constant_a-PRODUCT-True] test/integration/test_integration__io.py::test_build_subdataset_filter[netcdf:S5P_NRTI_L2__NO2____20190513T181819_20190513T182319_08191_01_010301_20190513T185033.nc:/PRODUCT/tm5_constant_a-tm5_constant_a-/PRODUCT-True] test/integration/test_integration__io.py::test_open_group_filter__missing[open_rasterio] test/integration/test_integration__io.py::test_open_group_filter__missing[open_rasterio_engine] test/integration/test_integration__io.py::test_serialization test/integration/test_integration__io.py::test_utm test/integration/test_integration__io.py::test_notransform test/integration/test_integration__io.py::test_indexing test/integration/test_integration__io.py::test_caching test/integration/test_integration__io.py::test_chunks test/integration/test_integration__io.py::test_pickle_rasterio test/integration/test_integration__io.py::test_no_mftime test/integration/test_integration__io.py::test_rasterio_environment test/integration/test_integration__io.py::test_rasterio_vrt test/integration/test_integration__io.py::test_rasterio_vrt_with_transform_and_size test/integration/test_integration__io.py::test_rasterio_vrt_with_src_crs test/integration/test_integration__io.py::test_open_cog test/integration/test_integration__io.py::test_notgeoreferenced_warning[open_rasterio] test/integration/test_integration__io.py::test_notgeoreferenced_warning[open_rasterio_engine] test/integration/test_integration__io.py::test_non_rectilinear test/integration/test_integration__io.py::test_non_rectilinear__load_coords[open_rasterio] test/integration/test_integration__io.py::test_non_rectilinear__load_coords[open_rasterio_engine] test/integration/test_integration__io.py::test_non_rectilinear__skip_parse_coordinates[open_rasterio] test/integration/test_integration__io.py::test_non_rectilinear__skip_parse_coordinates[open_rasterio_engine] test/integration/test_integration_rioxarray.py::test_pad_box[open_rasterio] test/integration/test_integration_rioxarray.py::test_pad_box[modis_clip3] test/integration/test_integration_rioxarray.py::test_pad_box[open_rasterio_engine] test/integration/test_integration_rioxarray.py::test_clip_box[open_rasterio] test/integration/test_integration_rioxarray.py::test_clip_box[modis_clip3] test/integration/test_integration_rioxarray.py::test_clip_box[open_rasterio_engine] test/integration/test_integration_rioxarray.py::test_clip_box__auto_expand[open_rasterio] test/integration/test_integration_rioxarray.py::test_clip_box__auto_expand[modis_clip3] test/integration/test_integration_rioxarray.py::test_clip_box__auto_expand[open_rasterio_engine] test/integration/test_integration_rioxarray.py::test_clip_box__nodata_error[open_rasterio] test/integration/test_integration_rioxarray.py::test_clip_box__nodata_error[modis_clip3] test/integration/test_integration_rioxarray.py::test_clip_box__nodata_error[open_rasterio_engine] test/integration/test_integration_rioxarray.py::test_slice_xy[open_rasterio] test/integration/test_integration_rioxarray.py::test_slice_xy[modis_clip3] test/integration/test_integration_rioxarray.py::test_slice_xy[open_rasterio_engine] test/integration/test_integration_rioxarray.py::test_clip_geojson[open_rasterio-True] test/integration/test_integration_rioxarray.py::test_clip_geojson[open_rasterio-False] test/integration/test_integration_rioxarray.py::test_clip_geojson[open_func1-True] test/integration/test_integration_rioxarray.py::test_clip_geojson[open_func1-False] test/integration/test_integration_rioxarray.py::test_clip_geojson[open_func2-True] test/integration/test_integration_rioxarray.py::test_clip_geojson[open_func2-False] test/integration/test_integration_rioxarray.py::test_clip_geojson[open_rasterio_engine-True] test/integration/test_integration_rioxarray.py::test_clip_geojson[open_rasterio_engine-False] test/integration/test_integration_rioxarray.py::test_clip_geojson[open_func4-True] test/integration/test_integration_rioxarray.py::test_clip_geojson[open_func4-False] test/integration/test_integration_rioxarray.py::test_clip_geojson[open_func5-True] test/integration/test_integration_rioxarray.py::test_clip_geojson[open_func5-False] test/integration/test_integration_rioxarray.py::test_transform_bounds[open_rasterio] test/integration/test_integration_rioxarray.py::test_transform_bounds[open_func3] test/integration/test_integration_rioxarray.py::test_transform_bounds[open_rasterio_engine] test/integration/test_integration_rioxarray.py::test_transform_bounds[open_func5] test/integration/test_integration_rioxarray.py::test_reproject_with_shape[open_rasterio_engine] test/integration/test_integration_rioxarray.py::test_reproject__scalar_coord[open_rasterio] test/integration/test_integration_rioxarray.py::test_reproject__scalar_coord[open_rasterio_engine] test/integration/test_integration_rioxarray.py::test_interpolate_na[open_rasterio_engine] test/integration/test_integration_rioxarray.py::test_interpolate_na__nodata_filled[open_rasterio_engine] test/integration/test_integration_rioxarray.py::test_missing_spatial_dimensions test/integration/test_integration_rioxarray.py::test_set_spatial_dims test/integration/test_integration_rioxarray.py::test_set_spatial_dims__missing test/integration/test_integration_rioxarray.py::test_crs_empty_dataset test/integration/test_integration_rioxarray.py::test_crs_setter test/integration/test_integration_rioxarray.py::test_crs_setter__copy test/integration/test_integration_rioxarray.py::test_crs_writer__array__copy test/integration/test_integration_rioxarray.py::test_crs_writer__array__inplace test/integration/test_integration_rioxarray.py::test_crs_writer__dataset__copy test/integration/test_integration_rioxarray.py::test_crs_writer__dataset__inplace test/integration/test_integration_rioxarray.py::test_crs_writer__missing test/integration/test_integration_rioxarray.py::test_crs__dataset__different_crs test/integration/test_integration_rioxarray.py::test_clip_missing_crs test/integration/test_integration_rioxarray.py::test_reproject_missing_crs test/integration/test_integration_rioxarray.py::test_reproject_resolution_and_shape_transform test/integration/test_integration_rioxarray.py::test_reproject_transform_missing_shape test/integration/test_integration_rioxarray.py::test_crs_get_custom test/integration/test_integration_rioxarray.py::test_get_crs_dataset test/integration/test_integration_rioxarray.py::test_write_crs_cf test/integration/test_integration_rioxarray.py::test_write_crs_cf__disable_grid_mapping test/integration/test_integration_rioxarray.py::test_write_crs__missing_geospatial_dims test/integration/test_integration_rioxarray.py::test_get_crs_dataset__nonstandard_grid_mapping test/integration/test_integration_rioxarray.py::test_nodata_setter test/integration/test_integration_rioxarray.py::test_nodata_setter__copy test/integration/test_integration_rioxarray.py::test_nodata_writer__array__copy test/integration/test_integration_rioxarray.py::test_nodata_writer__array__inplace test/integration/test_integration_rioxarray.py::test_nodata_writer__missing test/integration/test_integration_rioxarray.py::test_nodata_writer__remove test/integration/test_integration_rioxarray.py::test_nodata_writer__different_dtype[-1.1_0] test/integration/test_integration_rioxarray.py::test_nodata_writer__different_dtype[-1.1_1] test/integration/test_integration_rioxarray.py::test_isel_window test/integration/test_integration_rioxarray.py::test_write_pyproj_crs_dataset test/integration/test_integration_rioxarray.py::test_missing_transform_bounds test/integration/test_integration_rioxarray.py::test_missing_transform_resolution test/integration/test_integration_rioxarray.py::test_write_transform__from_read test/integration/test_integration_rioxarray.py::test_write_transform test/integration/test_integration_rioxarray.py::test_write_read_transform__non_rectilinear test/integration/test_integration_rioxarray.py::test_write_read_transform__non_rectilinear__warning test/integration/test_integration_rioxarray.py::test_missing_transform test/integration/test_integration_rioxarray.py::test_grid_mapping_default test/integration/test_integration_rioxarray.py::test_estimate_utm_crs test/integration/test_integration_rioxarray.py::test_estimate_utm_crs__missing_crs test/integration/test_integration_rioxarray.py::test_estimate_utm_crs__out_of_bounds test/integration/test_integration_rioxarray.py::test_interpolate_na_missing_nodata