@@ -356,6 +356,14 @@ def _nested_combine(
356356 # Check that the inferred shape is combinable
357357 _check_shape_tile_ids (combined_ids )
358358
359+ # Promote any DataArrays to Datasets
360+ for id , obj in combined_ids .items ():
361+ if isinstance (obj , DataArray ):
362+ if obj .name is None :
363+ combined_ids [id ] = obj ._to_temp_dataset ()
364+ else :
365+ combined_ids [id ] = obj .to_dataset ()
366+
359367 # Apply series of concatenate or merge operations along each dimension
360368 combined = _combine_nd (
361369 combined_ids ,
@@ -570,31 +578,59 @@ def combine_nested(
570578 merge
571579 combine_by_coords
572580 """
573-
574- mixed_datasets_and_arrays = any (
575- isinstance (obj , Dataset ) for obj in iterate_nested (datasets )
576- ) and any (
577- isinstance (obj , DataArray ) and obj .name is None
578- for obj in iterate_nested (datasets )
579- )
580- if mixed_datasets_and_arrays :
581- raise ValueError ("Can't combine datasets with unnamed arrays." )
581+
582+ # TODO deprecation cycle to change the name of this argument...
583+ data_objects = datasets
582584
583585 if isinstance (concat_dim , (str , DataArray )) or concat_dim is None :
584586 concat_dim = [concat_dim ]
585587
586- # The IDs argument tells _nested_combine that datasets aren't yet sorted
587- return _nested_combine (
588- datasets ,
589- concat_dims = concat_dim ,
590- compat = compat ,
591- data_vars = data_vars ,
592- coords = coords ,
593- ids = False ,
594- fill_value = fill_value ,
595- join = join ,
596- combine_attrs = combine_attrs ,
597- )
588+ objs_are_unnamed_dataarrays = [
589+ isinstance (data_object , DataArray ) and data_object .name is None
590+ for data_object in iterate_nested (data_objects )
591+ ]
592+ if any (objs_are_unnamed_dataarrays ):
593+ if all (objs_are_unnamed_dataarrays ):
594+ # Combine into a single larger DataArray
595+ unnamed_arrays = data_objects
596+
597+ combined_temp_dataset = _nested_combine (
598+ unnamed_arrays ,
599+ concat_dims = concat_dim ,
600+ compat = compat ,
601+ data_vars = data_vars ,
602+ coords = coords ,
603+ ids = False ,
604+ fill_value = fill_value ,
605+ join = join ,
606+ combine_attrs = combine_attrs ,
607+ )
608+ return DataArray ()._from_temp_dataset (combined_temp_dataset )
609+ else :
610+ # Must be a mix of unnamed dataarrays with either named dataarrays or with datasets
611+ # Can't combine these as we wouldn't know whether to merge or concatenate the arrays
612+ raise ValueError (
613+ "Can't automatically combine unnamed dataarrays with either named dataarrays or datasets."
614+ )
615+ else :
616+ # Promote any named DataArrays to single-variable Datasets to simplify combining
617+ # data_objects = [
618+ # obj.to_dataset() if isinstance(obj, DataArray) else obj
619+ # for obj in data_objects
620+ # ]
621+
622+ # The IDs argument tells _nested_combine that datasets aren't yet sorted
623+ return _nested_combine (
624+ data_objects ,
625+ concat_dims = concat_dim ,
626+ compat = compat ,
627+ data_vars = data_vars ,
628+ coords = coords ,
629+ ids = False ,
630+ fill_value = fill_value ,
631+ join = join ,
632+ combine_attrs = combine_attrs ,
633+ )
598634
599635
600636def vars_as_keys (ds ):
@@ -700,7 +736,6 @@ def combine_by_coords(
700736 ----------
701737 data_objects : sequence of xarray.Dataset or sequence of xarray.DataArray
702738 Data objects to combine.
703-
704739 compat : {"identical", "equals", "broadcast_equals", "no_conflicts", "override"}, optional
705740 String indicating how to compare variables of the same name for
706741 potential conflicts:
0 commit comments